package org.example.basic;

import java.io.*;
import java.util.*;

public class ScalabilityTest {

    public static List<Edge> nodeSelect(int num, int end, List<Edge> edgeList) {
        Random random = new Random();
        List<Edge> edgeSelect = new ArrayList<>();
        boolean[] nodeSelect = new boolean[end + 1];
        int x;
        System.out.printf("共选取%d个顶点，分别为\n", num);
        for (int i = 0; i < num; ++i) {
            x = random.nextInt(end + 1);
            while (nodeSelect[x]) {
                x = random.nextInt(end + 1);
            }
//            System.out.printf("%d ", x);
            nodeSelect[x] = true;
        }
//        System.out.println();
//        System.out.println("这些顶点组成的诱导子图包含的边如下：");
        int tot = 0;
        for (Edge edge : edgeList) {
            if (nodeSelect[edge.getU()] && nodeSelect[edge.getV()]) {
                edgeSelect.add(edge);
                ++tot;
//                System.out.printf("(%d,%d)\n", edge.getU(), edge.getV());
            }
        }
        System.out.printf("共包含%d条边\n", tot);
        return edgeSelect;
    }

    public static void main(String[] args) {

        int x;
        Scanner scanner = new Scanner(System.in);
        x = scanner.nextInt();
        String str;
        if (x == 1) {
            str = "core";
        } else {
            str = "truss";
        }

        String inputFileName = "./src/main/java/org/example/data/com-dblp.ungraph.txt";
        String outputFileName = "./src/main/java/org/example/data/k-dblp-percent-" + str + "-result.txt";
        String regex = "\t";

        String timeOutputFileName = "./src/main/java/org/example/data/k-dblp-percent-time-" + str + ".txt";

        if (x == 1) {
            CoreCommunity coreCommunity = new CoreCommunity();
            coreCommunity.readFile(inputFileName, regex);
            int start = coreCommunity.getStart();
            int end = coreCommunity.getEnd();
            List<Edge> edgeList = coreCommunity.getEdgeList();

            for (int l = 1; l <= 5; ++l) {
                long timestamp1 = new Date().getTime(); // 获取当前时间的时间戳
                coreCommunity = new CoreCommunity();
                coreCommunity.setStart(start);
                coreCommunity.setEnd(end);
                int num = end * l / 5;
                // 选取num个顶点
                List<Edge> edges = nodeSelect(num, end, edgeList);

                boolean[] kSelect = new boolean[200];
                Random random = new Random();
                for (int t = 1; t <= 10; ++t) {
                    coreCommunity.setEdgeList(edges);
                    int k = random.nextInt(120) + 1;
                    while (kSelect[k]) {
                        k = random.nextInt(120) + 1;
                    }
                    kSelect[k] = true;
                    coreCommunity.kCoreCommunitySearchNoRead(k, outputFileName);
                }

                long timestamp2 = new Date().getTime(); // 获取另一个时间的时间戳
                long diffSeconds = (timestamp2 - timestamp1); // 计算时间差（单位：毫秒）

                try {
                    FileWriter fileWriter = new FileWriter(timeOutputFileName, true);
                    String line = l + " " + diffSeconds + "\n";
                    fileWriter.write(line);
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        } else {
            TrussCommunity trussCommunity = new TrussCommunity();
            trussCommunity.readFile(inputFileName, regex);
            int start = trussCommunity.getStart();
            int end = trussCommunity.getEnd();
            List<Edge> edgeList = trussCommunity.getEdgeList();

            for (int l = 1; l <= 5; ++l) {
                long timestamp1 = new Date().getTime(); // 获取当前时间的时间戳
                trussCommunity = new TrussCommunity();
                trussCommunity.setStart(start);
                trussCommunity.setEnd(end);
                int num = end * l / 5;
                // 选取num个顶点
                List<Edge> edges = nodeSelect(num, end, edgeList);

                boolean[] kSelect = new boolean[200];
                Random random = new Random();
                for (int t = 1; t <= 10; ++t) {
                    List<Edge> currentEdgeList = new ArrayList<>(edges);
                    trussCommunity.setEdgeList(currentEdgeList);
                    int k = random.nextInt(120) + 1;
                    while (kSelect[k]) {
                        k = random.nextInt(120) + 1;
                    }
                    kSelect[k] = true;

                    trussCommunity.kTrussCommunitySearchNoRead(k, outputFileName);
                }

                long timestamp2 = new Date().getTime(); // 获取另一个时间的时间戳
                long diffSeconds = (timestamp2 - timestamp1); // 计算时间差（单位：毫秒）

                try {
                    FileWriter fileWriter = new FileWriter(timeOutputFileName, true);
                    String line = l + " " + diffSeconds + "\n";
                    fileWriter.write(line);
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

    }
}
